package org.darkforest.task;

import org.darkforest.dao.mapper.OrderStatisticsMapper;
import org.darkforest.dao.mapper.SellerMapper;
import org.darkforest.model.OrderStatistics;
import org.darkforest.model.Seller;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * Created by Administrator on 2017/5/17.
 */
@Component
public class MerchantStatisticsQuarz {

    private org.slf4j.Logger logger = LoggerFactory.getLogger(MerchantStatisticsQuarz.class);


    @Autowired
    private OrderStatisticsMapper orderStatisticsMapper;

    @Autowired
    private SellerMapper sellerMapper;

    /**
     * 统计概览
     * 每天零点1分执行
     */
//    @Scheduled(cron = "0 0/1 * * * ?")
    @Scheduled(cron = "0 0 1 * * ?")

    public void statisticsInTheOverview(){

        Calendar cal=Calendar.getInstance();
        cal.add(Calendar.DATE, -1);
        Date time=cal.getTime();

        OrderStatistics orderStatistics = new OrderStatistics();

        //查询所有店铺信息
        List<Seller> sellerList = sellerMapper.getSellerList();
        for (Seller seller : sellerList) {
            //店铺编号
            Integer storeId = seller.getStoreId();

            orderStatistics = new OrderStatistics();
            orderStatistics.setsStoreId(storeId);
            orderStatistics.setsDate(new SimpleDateFormat("yyyy-MM-dd").format(time));

            //昨日订单总金额
            String zrddje_sql = "SELECT IFNULL(SUM(order_amount),0.00) FROM bw_order WHERE store_id = " + storeId + " and order_state in ('20','30','40') and TO_DAYS(NOW()) - TO_DAYS(FROM_UNIXTIME(add_time)) <= 1";
            String zrddje = orderStatisticsMapper.getBySql(zrddje_sql);
            orderStatistics.setsOrderAmount(new BigDecimal(zrddje));

            //昨日下单会员数
            String zrxdhys_sql = "SELECT IFNULL(SUM(t.member_count),0) FROM (SELECT COUNT(0) AS member_count FROM bw_order WHERE store_id = " + storeId + " and order_state in ('20','30','40') and TO_DAYS(NOW()) - TO_DAYS(FROM_UNIXTIME(add_time)) <= 1  GROUP BY buyer_id) AS t";
            String zrxdhys = orderStatisticsMapper.getBySql(zrxdhys_sql);
            orderStatistics.setsOrderMemberCount(Integer.parseInt(zrxdhys));

            //昨日下单量
            String zrxdl_sql = "SELECT COUNT(0)  FROM bw_order WHERE store_id = " + storeId + " and order_state in ('20','30','40') and TO_DAYS(NOW()) - TO_DAYS(FROM_UNIXTIME(add_time)) <= 1";
            String zrxdl = orderStatisticsMapper.getBySql(zrxdl_sql);
            orderStatistics.setsOrderCount(Integer.parseInt(zrxdl));

            //昨日下单商品数
            String zrxdsps_sql = "SELECT IFNULL(SUM(tmp.goods_num),0) FROM (\n" +
                    "SELECT \n" +
                    "  t1.goods_num \n" +
                    "FROM\n" +
                    "  bw_order t \n" +
                    "  LEFT JOIN bw_order_goods t1 \n" +
                    "    ON t.order_id = t1.order_id \n" +
                    "  and t.store_id = " + storeId + " and t.order_state in ('20','30','40') and  TO_DAYS(NOW()) - TO_DAYS(FROM_UNIXTIME(t1.add_time)) <= 1" +
                    "  )  AS tmp";

            String zrxdsps = orderStatisticsMapper.getBySql(zrxdsps_sql);
            orderStatistics.setsOrderGoodsCount(Integer.parseInt(zrxdsps));

            //商品总数
            String all_goods_count_sql = "select count(0) from bw_goods_common where store_id = " + storeId;
            String all_goods_count = orderStatisticsMapper.getBySql(all_goods_count_sql);
            orderStatistics.setsAllGoosCount(Integer.parseInt(all_goods_count));

            //在售商品总数
            String insale_goods_count_sql = "select count(0) from bw_goods_common where store_id = " + storeId + " and goods_state = 1";
            String insale_goods_count = orderStatisticsMapper.getBySql(insale_goods_count_sql);
            orderStatistics.setsInSaleCount(Integer.parseInt(insale_goods_count));

            //下架总数
            String outsale_goods_count_sql = "select count(0) from bw_goods_common where store_id = " + storeId + " and goods_state = 0";
            String outsale_goods_count = orderStatisticsMapper.getBySql(outsale_goods_count_sql);
            orderStatistics.setsOutSaleCount(Integer.parseInt(outsale_goods_count));

            logger.info("#######店铺编号=" + storeId);
            logger.info("#######昨日订单总金额=" + zrddje);
            logger.info("#######昨日下单会员数=" + zrxdhys);
            logger.info("#######昨日下单量=" + zrxdl);
            logger.info("#######昨日下单商品数=" + zrxdsps);
            logger.info("#######商品总数=" + all_goods_count);
            logger.info("#######在售商品总数=" + insale_goods_count);
            logger.info("#######下架总数=" + outsale_goods_count);

            orderStatisticsMapper.insertSelective(orderStatistics);
        }

    }

}
